package com.play.game.mydb;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.play.game.mydb.db.MyDatabsesHelper;

public class MainActivity extends AppCompatActivity {

    private MyDatabsesHelper databsesHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = (Button) findViewById(R.id.bt);
        Button add = (Button) findViewById(R.id.add);
        Button update = (Button) findViewById(R.id.update);
        Button delete = (Button) findViewById(R.id.delete);
        Button queryss = (Button) findViewById(R.id.query);

        databsesHelper = new MyDatabsesHelper(MainActivity.this);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                databsesHelper.getReadableDatabase();
            }
        });

        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SQLiteDatabase db = databsesHelper.getReadableDatabase();
                ContentValues values = new ContentValues();
                values.put("name", "Hashiqi");
                values.put("author", "husky");
                values.put("pages", 999);
                values.put("price", 100);
                db.insert("Book", null, values);
                values.clear();
                values.put("name", "Samoye");
                values.put("author", "samoye");
                values.put("pages", 599);
                values.put("price", 120);
                db.insert("Book", null, values);


            }
        });
        update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SQLiteDatabase db = databsesHelper.getReadableDatabase();
                ContentValues values=new ContentValues();
                values.put("name","Ma");
                values.put("author", "ma");
                values.put("pages", 899);
                values.put("price", 420);
                db.update("Book",values,"id=?",new String[]{"6"});

            }
        });
        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SQLiteDatabase db = databsesHelper.getReadableDatabase();
                db.delete("Book","name=?",new String[]{"Ma"});

            }
        });
        queryss.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                SQLiteDatabase db = databsesHelper.getReadableDatabase();
                Cursor cursor = db.query("Book", null, null, null, null, null, null);
                if (cursor.moveToFirst()) {
                    do {
                        String name = cursor.getString(cursor.getColumnIndex("name"));
                        String author = cursor.getString(cursor.getColumnIndex("author"));
                        int pages = cursor.getInt(cursor.getColumnIndex("pages"));
                        double price = cursor.getDouble(cursor.getColumnIndex("price"));


                        Log.i("cuesorcheck", "onClick: " + name + "~" + author + "~" + pages + "~" + price);
                        Toast.makeText(MainActivity.this, "" + name + "~" + author + "~" + pages + "~" + price, Toast.LENGTH_SHORT).show();

                    } while (cursor.moveToNext());

                }


                cursor.close();

            }
        });
    }
}
